﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace CloudAppMonitor.Library
{
    internal static class Logging
    {
        public static void LogException(string source, Exception ex)
        {
            StringBuilder exceptionString = new StringBuilder();

            BuildExceptionString(ex, exceptionString);

            EventLog.WriteEntry(Monitor.EVENT_SOURCE, string.Format("{0} failed.\n\n{1}",source, exceptionString.ToString()), EventLogEntryType.Error);
        }

        private static void BuildExceptionString(Exception ex, StringBuilder exceptionString)
        {
            exceptionString.AppendFormat("{0}({1}):\n{2}", ex.Message, ex.GetType().Name, ex.StackTrace);

            if (ex.InnerException != null)
            {
                exceptionString.Append("\n\nInner Exception: ");

                BuildExceptionString(ex.InnerException, exceptionString);
            }
        }
    }
}
